package com.jiaquyun.jcyx.main.mine

import android.graphics.Typeface
import android.os.Bundle
import android.view.Gravity
import android.view.View
import android.widget.FrameLayout
import android.widget.LinearLayout
import android.widget.TextView
import androidx.lifecycle.LifecycleOwner
import androidx.lifecycle.Observer
import com.blankj.utilcode.util.ColorUtils
import com.blankj.utilcode.util.ConvertUtils
import com.jiaquyun.jcyx.R
import com.jiaquyun.jcyx.jetpack.mode.MineViewModel
import com.module.lemlin.http.Status
import com.module.lemlin.http.msg

import com.module.lemlin.owner.OwnerViewModeFragment
import kotlinx.android.synthetic.main.fragment_integral_details.*


class IntegralDetailsFragment : OwnerViewModeFragment<MineViewModel>() {

    companion object {
        const val KEY_COLLECT_ACTION = "KEY_COLLECT_ACTION"
        fun newInstance(action: Int): IntegralDetailsFragment {
            val bundle = Bundle().apply {
                putInt(KEY_COLLECT_ACTION, action)
            }
            return IntegralDetailsFragment().apply {
                arguments = bundle
            }
        }
    }

    private val mScoreListAdapter = ScoreListAdapter()

    private val mAction: Int by lazy {
        arguments?.getInt(KEY_COLLECT_ACTION) ?: 1
    }


    override fun viewModel(): MineViewModel {
        return createViewModel()
    }

    override val layoutResId: Int
        get() = R.layout.fragment_integral_details

    override fun initView() {
        rvIntegralDetailsBody.adapter = mScoreListAdapter

        mSmartRefreshLayout.setEnableRefresh(false)
        mSmartRefreshLayout.setEnableLoadMore(false)
        mSmartRefreshLayout.setOnLoadMoreListener {
            viewModel.userScoreLog(mAction, isLoadMore = true)
        }
    }

    override fun initData() {
        viewModel.userScoreLog(mAction)
    }

    override fun initViewMode() {

        viewModel.userScoreLogLiveDate.observe(this as LifecycleOwner, Observer { response ->
            if (response.status == Status.FAILURE) {
                toast(response.error?.msg ?: "位置错误")
                mSmartRefreshLayout.finishLoadMore((false))
            } else if (response.status == Status.SUCCESS) {
                mSmartRefreshLayout.finishLoadMore()
            }
            if (response.status != Status.SUCCESS)
                return@Observer
            response.data?.let {
                if (it.data.size == 0) {
                    mScoreListAdapter.setEmptyView(createEmptyView())
                }
                if (it.loadMore) {
                    mScoreListAdapter.addData(it.data)
                } else {
                    mScoreListAdapter.setNewInstance(it.data)
                }
                if (it.data.size < viewModel.rows) {
                    mScoreListAdapter.setFooterView(createFooterView())
                    mSmartRefreshLayout.setEnableLoadMore(false)
                } else {
                    mScoreListAdapter.removeAllFooterView()
                    mSmartRefreshLayout.setEnableLoadMore(true)
                }
            }
        })
    }


    private fun createEmptyView(): View {
        return layoutInflater.inflate(R.layout.view_empty_error, null)
    }

    private fun createFooterView(): View = TextView(requireContext()).apply {
        layoutParams = FrameLayout.LayoutParams(
            LinearLayout.LayoutParams.MATCH_PARENT,
            ConvertUtils.dp2px(30f)
        )
        gravity = Gravity.CENTER
        textSize = 13f
        text = "没有更多数据"
        setTypeface(null, Typeface.BOLD)
        setTextColor(ColorUtils.getColor(R.color.FF_666666))
    }
}